# -*- coding: utf-8 -*-
import scrapy


class CarspecsSpider(scrapy.Spider):
    name = 'carspecs_eff'
    start_urls = ['https://www.carfolio.com/car-makes/']
    dic = {}
    data_mk = ['ACURA', 'ALFA ROMEO', 'ASTON MARTIN', 'AUDI', 'BEIJING AUTO',
               'BENTLEY', 'BMW', 'BRILLIANCE', 'BUICK', 'BYD', 'CADILLAC',
               'CATERHAM', 'CHANA', 'CHANGHE', 'CHERY', 'CHEVROLET', 'CHRYSLER',
               'CITROEN', 'DACIA', 'DAEWOO', 'DAIHATSU', 'DATSUN', 'DODGE',
               'DONGFENG', 'DS', 'FAW', 'FERRARI', 'FIAT', 'FORD', 'GEELY',
               'GREAT WALL', 'GREEN WHEEL', 'HAIMA', 'HONDA', 'HUDSON', 'HUMMER',
               'HYUNDAI', 'INFINITI', 'JAC', 'JAGUAR', 'JEEP', 'KARRY', 'KIA',
               'LANCIA', 'LAND ROVER', 'LEXUS', 'LIFAN', 'LINCOLN', 'MAHINDRA',
               'MASERATI', 'MAZDA', 'MCLAREN', 'MERCEDES-BENZ', 'MERCURY', 'MG',
               'MINI', 'MITSUBISHI', 'NISSAN', 'PEUGEOT', 'PLYMOUTH', 'PONTIAC',
               'PORSCHE', 'RAM', 'RENAULT', 'ROLLS-ROYCE', 'ROVER', 'SATURN',
               'SCION', 'SEAT', 'SKODA', 'SMART', 'STUDEBAKER', 'SUBARU',
               'SUZUKI', 'TESLA', 'TOYOTA', 'UNSPECIFIED', 'VOLKSWAGEN', 'VOLVO',
               'WESTFIELD', 'ZEDRIV', 'ACURA-GUANGZHOU', 'AIWAYS', 'ALPINA',
               'ARCFOX', 'AUDI-FAW', 'BAIC YINXIANG', 'BAOJUN', 'BESTURN', 'BISU',
               'BJEV', 'BMW-BRILLIANCE', 'BORGWARD', 'BUFORI', 'BUICK-SHANGHAI',
               'BYVIN', 'CADILLAC-SHANGHAI', 'CHANGAN', 'CHANGJIANG',
               'CHEVROLET-SHANGHAI', 'CHEVROLET-WULING', 'CHRYSLER-FUTIAN',
               'CIIMO', 'CITROEN-DONGFENG', 'COWIN', 'DAYUN', 'DEARCC', 'DENZA',
               'DODGE-SOUEAST', 'DONGFENG-ZNA', 'DORCEN', 'DS-CHANGAN', 'ENOVATE',
               'ENRANGER (YINGZHI)', 'EVERUS', 'EXEED', 'FIAT-GUANGZHOU', 'FODAY',
               'FORD-CHANGAN', 'FORD-JMC', 'FOTON', 'FUJIAN', 'FUKANG',
               'GEELY-EMGRAND', 'GEELY-ENGLON', 'GEELY-GLEAGLE', 'GEOMETRY',
               'GMC', 'GOLDEN DRAGON', 'GONOW', 'GUANGZHOU AUTO', 'HAFEI',
               'HANTENG', 'HAWTAI', 'HIPHI', 'HONDA-DONGFENG', 'HONDA-GUANGZHOU',
               'HONGQI', 'HONGXING', 'HORKI', 'HUANGHAI', 'HUNKT', 'HYCAN',
               'HYUNDAI-BEIJING', 'INFINITI-DONGFENG', 'ISUZU-JIANGXI',
               'ISUZU-QINGLING', 'JAC-ANCHI', 'JAGUAR-CHERY', 'JEEP-GUANGZHOU',
               'JETOUR', 'JETTA', 'JIABAO (SICHUAN)', 'JIANGLING EV',
               'JIANGNAN (HUNAN)', 'JINBEI', 'JINCHENG', 'JMC', 'JONWAY',
               'JOYLONG', 'KAICHENG', 'KANDI', 'KARMA', 'KAWEI', 'KIA-YUEDA',
               'KTM', 'LADA', 'LAMBORGHINI', 'LAND ROVER-CHERY', 'LANDWIND',
               'LEAPMOTOR', 'LEOPAARD', 'LEVC', 'LEVDEO', 'LI', 'LIANHUA',
               'LINCOLN-CHANGAN', 'LINGBAO', 'LINKTOUR', 'LOTUS', 'LUXGEN',
               'LYNK & CO', 'MAPLE', 'MAXUS', 'MAYBACH', 'MAZDA-CHANGAN',
               'MAZDA-FAW', 'MERCEDES-BEIJING', 'MERCEDES-FUJIAN', 'MINAN AUTO',
               'MINGJUN', 'MITSUBISHI-GAC', 'MITSUBISHI-SOUEAST', 'MITSUOKA',
               'MORGAN', 'NETA', 'NEVS', 'NEW-DADI (XINDADI)', 'NIO',
               'NISSAN-DONGFENG', 'NOBLE', 'OLEY', 'OPEL', 'ORA', 'OSSAN',
               'PAGANI', 'PEUGEOT-DONGFENG', 'POCCO', 'POLESTAR', 'PROTON',
               'QIANTU', 'QOROS', 'RELY (WEILIN)', 'RENAULT-DONGFENG', 'RIICH',
               'ROEWE', 'SAAB', 'SERES', 'SHAANXI (HANJIANG)',
               'SHANGHAI MAPLE (SMA)', 'SHOUWANG', 'SHUANGHUAN', 'SINGULATO',
               'SINOGOLD', 'SITECH', 'SKODA-SHANGHAI', 'SKYWELL', 'SOUEAST',
               'SPYKER', 'SSANGYONG', 'SUDA', 'SUZUKI-CHANGAN', 'SUZUKI-CHANGHE',
               'SWM', 'TANTUS (TIANYU)', 'TIANMA', 'TIANQI MEIYA',
               'TONGJIA (SHAANXI)', 'TOYOTA-FAW', 'TOYOTA-GUANGZHOU', 'TRAUM',
               'UAZ', 'VENUCIA', 'VICTORY (YICHY)', 'VIEW', 'VOLKSWAGEN-FAW',
               'VOLKSWAGEN-SHANGHAI', 'VOLVO-CHANGAN', 'VOLVO-GEELY', 'WEY',
               'WM MOTOR', 'WULING', 'XIALI', 'XINKAI AUTO', 'XPENG', 'YEMA',
               'YOUNGMAN', 'YUDO', 'YULON', 'ZHIDOU', 'ZHONGHUA', 'ZINORO',
               'ZOTYE', 'ZX AUTO (ZHONGXING)']

    def parse(self, response):
        for qq in response.xpath('//div/a[@class="man"]'):
            if qq.xpath('text()').get().upper() in self.data_mk:
                url = "https://www.carfolio.com" + qq.xpath('@href').get()
                yield scrapy.Request(url, callback=self.parse_mod)
            else:
                pass

    def parse_mod(self, response):
        for tt in response.xpath('//div[@class="grid"]/div'):
            url2 = "https://www.carfolio.com" + tt.xpath('div[@class="card-head"]/a/@href').get()
            yield scrapy.Request(url2, callback=self.parse_emi)

    def parse_emi(self, response):
        idc = response.xpath('//div/label/input/@id').get()
        try:
            eff = response.xpath('//tr/td[contains(., "l/100km")]/text()').get()
            emi = response.xpath('//tr/td[contains(., "g/km")]/text()').get()
        except IndexError:
            eff = 0
            emi = 0

        yield {idc: {
            'eff': eff,
            'emi': emi,
        }
        }
